/* 
 * File:   Intersection.cpp
 * Author: mikey
 * 
 * Created on August 25, 2010, 10:03 PM
 */

#include "Intersection.h"

Intersection::Intersection() {
    hRoad = Road();
    vRoad = Road();
    hLight = TrafficLight();
    vLight = TrafficLight();
}

Intersection::Intersection(int hLanes, int vLanes) {
    hRoad = Road(hLanes);
    vRoad = Road(vLanes);
    hLight = TrafficLight();
    vLight = TrafficLight();
}

void Intersection::setHLanes(int hLanes) {
	hRoad = Road(hLanes);
}
void Intersection::setVLanes(int vLanes) {
	vRoad = Road(vLanes);
}
void Intersection::setHProbability(float newHProbability) {
    hRoad.setRoadProbability(newHProbability);
}

void Intersection::setVProbability(float newVProbability) {
    vRoad.setRoadProbability(newVProbability);
}

Road Intersection::getHRoad() {
    return hRoad;
}

Road Intersection::getVRoad() {
    return vRoad;
}

void Intersection::runCycle() {
    
}

string Intersection::getState() {
    string state = "";
    bool toggle = false;
    int vFullLane = vRoad.getNumLanes();
    int vHalfLane = vRoad.getNumLanes() / 2;
	int hFullLane = hRoad.getNumLanes();
	int hHalfLane = hRoad.getNumLanes() / 2;
    int c = 0;


    // Do top vCarPositions.
    for(int i=0;i<9;i++) {
        state = state + vSpace + "|";
        for(c=0;c<vFullLane;c++) {
            state += vRoad.getLane(c).GetCarPosition(i).getCarSymbol();
			if (c != vFullLane - 1){
				if (toggle) {
					state += "|";
				}
				else {
					state += " ";
				}
			}
		}
		state += "|\n";
		toggle = !toggle;
	}



    // Do 10th vCarPosition
    state = state + hBar + "|";
    for(c=0;c<vFullLane;c++) {
        state += vRoad.getLane(c).GetCarPosition(9).getCarSymbol();
		if (c != vFullLane - 1){
			state += "|";
		}
    }
    state = state + "|" + hBar + "\n";




    // Do Horizontal Lanes
    for(c=0;c<hFullLane;c++){
        for(int i=0;i<10;i++) {
	           state += hRoad.getLane(c).GetCarPosition(i).getCarSymbol();
        }
        for(int s=0;s<hFullLane;s++){
            state += " ";
        }
        for(int i=10;i<20;i++) {
            state += hRoad.getLane(c).GetCarPosition(i).getCarSymbol();
        }
        state += "\n";
		if  (c != hFullLane -1){
			state += hRoadDivide;
			for(int s=0;s<(vFullLane*2-1);s++){
				state += " ";
			}
			state += hRoadDivide;
			state += "\n";
		}
    }




    // Do 11th vCarPosition
    state = state + hBar + "|";
    for(c=0;c<vFullLane;c++) {
        state += vRoad.getLane(c).GetCarPosition(10).getCarSymbol();
		if (c != vFullLane - 1){
			state += "|";
		}
    }
    state = state + "|" + hBar + "\n";



	toggle = false;
    // Do 12-20 vCarPosition
    for(int i=11;i<20;i++) {
        state = state + vSpace + "|";
        for(c=0;c<vFullLane;c++) {
            state += vRoad.getLane(c).GetCarPosition(i).getCarSymbol();
			if (c != vFullLane - 1){
				if (toggle) {
					state += "|";
				}
				else {
					state += " ";
				}
			}
		}
        state += "|\n";
        toggle = !toggle;
    }
    
    return state;
}